package com.hm.service.service.filtration;

import com.hm.service.mapper.keyword.KeywordMapper;
import com.hm.service.mapper.user.UserMapper;
import com.hm.service.pojo.db.KeywordBean;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


@Service
public class FiltrationService implements IFiltrationService {

    private static final Logger logger = LoggerFactory.getLogger(FiltrationService.class);

    @Qualifier("MainSqlSessionFactory")
    private final SqlSessionFactory sqlSessionFactory;

    public FiltrationService(SqlSessionFactory sqlSessionFactory) {
        this.sqlSessionFactory = sqlSessionFactory;
    }

    @Override
    public String interceptKey(String userId, String str) {
        //根据用户id查询关键字
        try (SqlSession session = sqlSessionFactory.openSession(true)) {
            KeywordMapper mapper = session.getMapper(KeywordMapper.class);
            HashMap<String, String> params = new HashMap<>();
            params.put("userId", userId);
            List<KeywordBean> keywordList = mapper.getKeywordList(params);
            if (keywordList == null || keywordList.isEmpty()) {
                return "";
            }
            int size = keywordList.size();
            for (int i = 0; i < size; i++) {
                KeywordBean keywordBean = keywordList.get(i);
                String keyword = keywordBean.keyword;
                //输入的问题包含关键字
                if (str.contains(keyword)) {
                    return "0";
                }
            }
            return "";
        }
    }
}
